package com.viewonimage.ta.lab;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class ViewonImageViewActivity extends Activity {

	private CustomImageView img;
	private Runnable mrun;
	private Canvas canvas;
	private Runnable mrun1;
	private Runnable onrun1;
	private Runnable onrun;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
//        		WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        
        setContentView(R.layout.main);
        //Sử dụng một đối tượng Imamage tạm để load động resource trong drawable/list_fire.xml 
        ImageView imgtemp = new ImageView(this);
        imgtemp.setBackgroundResource(R.drawable.list_fire);
        
        img = (CustomImageView)findViewById(R.id.customImageView1);
        //Đưa dữ liệu vào arraylist của lớp CustomImageView với nguồn là "imgtemp" 
        img.getArrayImage((AnimationDrawable)imgtemp.getBackground());
        Log.i("TA", "Getresource finisk");
       
        //Tạo mới đối tượng để sử dụng cho hàm updateImage
        onrun = new Runnable() {
			public void run() {
				img.invalidate();
			}
		};
        mrun = new Runnable() {
			private boolean FagRun=true;
			
			public void run() {
				int i = 0;
				while(FagRun){
					try {
						if(i>img.getListbm().length-1)
							i=0;
					
						updateImge();
						img.setIndex(i);
						img.setCheckruntext(false);
						Thread.sleep(200);
						i++;
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			private void updateImge() {
				// TODO Auto-generated method stub
				runOnUiThread(onrun);

			}
		};
		new Thread(mrun).start();
		
		//Tạo mới đối tượng để sử dụng cho hàm updateImage
		onrun1 = new Runnable() {
			public void run() {
				img.invalidate();
			}
		};
		//Runnable cho đốm lửa  2
		mrun1 = new Runnable() {
			private boolean FagRun=true;
			
			public void run() {
				int i = 0;
				while(FagRun){
					try {
						if(i>img.getListbm().length-1)
							i=0; 
							
					
						updateImge();
						img.setIndex(i);
						img.setCheckruntext(true);
						Thread.sleep(1000);
						i++;
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			
			private void updateImge() {
				// TODO Auto-generated method stub
				runOnUiThread(onrun1);
			}
		};
		new Thread(mrun1).start();
    }
    
}